extends Node2D
## 游戏结束信号
signal game_over
## 世界速度
@export var word_speed:int = 300
## 平台
@onready var moving_environment:Node2D = $Environment/Moving
@onready var collect_sound:AudioStreamPlayer = $"/root/World/Sound/CollectSound"
@onready var score_lable:Label = $"/root/World/HUD/UI/Score"
@onready var game_over_lable:Label=$"/root/World/HUD/UI/GameOver"
@onready var ammo_lable:Label = $"/root/World/HUD/UI/Ammo"
@onready var player:CharacterBody2D=$"/root/World/Player"
@onready var ground:Area2D = $"/root/World/Environment/Static/Ground"
## 平台资源 预加载
var platform=preload('res://scenes/platform.tscn')
var platform_collectible_singlel=preload('res://scenes/platform_collectible_singlel.tscn')
var platform_collectible_row=preload('res://scenes/platform_collectible_row.tscn')
var platform_collectible_rainbow=preload('res://scenes/platform_collectible_rainbow.tscn')
var platform_enemy = preload('res://scenes/platform_enemy.tscn')
var platform_collectible_ammo = preload('res://scenes/platform_collectible_ammo.tscn')
# 伪随机数
var rng=RandomNumberGenerator.new()
# 下一个平台位置
var last_platform_position:Vector2=Vector2.ZERO
# 下次生成时间
var next_spawn_time=0
## 分数
var score:int = 0
# 音频
var collectible_pitch:float=1.0
# 重置时间
var reset_collectble_pitch_time=0
#
var collectble_pitch_reset_intval=2000


func _ready():
	rng.randomize()
	player.player_died.connect(_on_player_died)
	ground.body_entered.connect(on_ground_body_entered)

func _process(delta):
	if not player.active:
		if Input.is_action_just_pressed('jump'):
			#reload_current_scene 重新加载当前活动的场景
			get_tree().reload_current_scene()
			return
		if Input.is_action_just_pressed('muen'):
			get_tree().change_scene_to_file("res://scenes/menu.tscn")
			return
	if Time.get_ticks_msec() >reset_collectble_pitch_time:
		collectible_pitch=1.0
	# Time.get_ticks_msec 引擎启动开始所经过的时间
	if Time.get_ticks_msec() >next_spawn_time:
		_spawn_next_platform()
	# 分数显示
	score_lable.text="Score: %s" % score
	ammo_lable.text="Ammo: %s" % player.ammo
func _spawn_next_platform():
	# 如果运行的时间大于>next_spawn_time 就生成新平台
	var availble_plaforms:Array=[
		platform,
		platform_collectible_singlel,
		platform_collectible_row,
		platform_collectible_rainbow,
		platform_enemy,
		platform_collectible_ammo
	]
	# pick_random 从目标数组中返回一个随机值
	var random_platform = availble_plaforms.pick_random()
	var new_platform=random_platform.instantiate()
	# 设置平台的位置
	if last_platform_position==Vector2.ZERO:
		new_platform.position=Vector2(400,0)
	else :
		# 随机生成平台位置
		var x:int=last_platform_position.x+rng.randi_range(450,550)
		var y:int=clamp(last_platform_position.y+rng.randi_range(-150,150),200,600)
		# 设置平台位置
		new_platform.position = Vector2(x,y)
		
	moving_environment.add_child(new_platform)
	last_platform_position=new_platform.position
	next_spawn_time+=word_speed

func _physics_process(delta):
	if not player.active:
		return
	# 平台向左移动
	moving_environment.position.x -= delta*word_speed

# 游戏得分
func add_score(value:int):
	score+=value
	collect_sound.set_pitch_scale(collectible_pitch)
	collect_sound.play()
	collectible_pitch += 0.1
	collectible_pitch=clamp(collectible_pitch,1.0,2.0)
	reset_collectble_pitch_time=Time.get_ticks_msec()+collectble_pitch_reset_intval

func _on_player_died():
	emit_signal('game_over')
	game_over_lable.text=game_over_lable.text % score
	game_over_lable.set_visible(true)

func on_ground_body_entered(body):
	if body.is_in_group('player'):
		player.die()
